﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.HumanResourcesDto;
using ThridGroup.ERP.DTO.ProductionSystem;
using ThridGroup.ERP.DTO.SupplierDto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
namespace ThridGroup.ERP.HumanResources
{
    public class OnboardingReviewServices : ApplicationService, IOnboardingReviewServices
    {
        private readonly IRepository<OnboardingReviewModel, int> _onboardingReviewServices;

        public OnboardingReviewServices(IRepository<OnboardingReviewModel, int> onboardingReviewServices) 
        {
            _onboardingReviewServices = onboardingReviewServices;
        }

        /// <summary>
        /// 添加入职申请审核
        /// </summary>
        /// <param name="onboardingReviewDto"></param>
        /// <returns></returns>
        public async Task<int> OnboardingReviewAsync(OnboardingReviewDto onboardingReviewDto)
        {
            var result = ObjectMapper.Map<OnboardingReviewDto, OnboardingReviewModel>(onboardingReviewDto);
            await _onboardingReviewServices.InsertAsync(result);
            return result.Id;
        }




        /// <summary>
        /// 入职申请审核（分页查询）
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">每页数量</param>
        /// <param name="personName">人员名称（模糊查询）</param>
        /// <param name="interviewPeople">面试官（模糊查询）</param>
        /// <returns>分页后的入职申请审核列表</returns>
        public async Task<PagedResultDto<OnboardingReviewListDto>> GetOnboardingReviewList(int page, int limit, string personName = null, string interviewPeople = null)
        {
            try
            {
                // 获取 IQueryable
                var queryable = await _onboardingReviewServices.GetQueryableAsync();

                // 应用筛选条件
                if (!string.IsNullOrWhiteSpace(personName))
                {
                    queryable = queryable.Where(x => x.PersonName.Contains(personName));
                }
                if (!string.IsNullOrWhiteSpace(interviewPeople))
                {
                    queryable = queryable.Where(x => x.InterviewPeople.Contains(interviewPeople));
                }

                // 获取总数
                var totalCount = queryable.Count();

                // 分页并排序
                var items = queryable.OrderByDescending(x => x.Id)
                    .Skip((page - 1) * limit)
                    .Take(limit)
                    .ToList();

                // 实体转 DTO
                var dtos = ObjectMapper.Map<List<OnboardingReviewModel>, List<OnboardingReviewListDto>>(items);

                return new PagedResultDto<OnboardingReviewListDto>(totalCount, dtos);
            }
            catch (Exception ex)
            {
                throw new Exception($"获取入职申请审核列表失败: {ex.Message}");
            }
        }

        public async Task DeleteOnboardingReviewAsync(int id)
        {
            await _onboardingReviewServices.DeleteAsync(id);
        }

        public async Task BatchDeleteOnboardingReviewAsync(List<int> ids)
        {
            await _onboardingReviewServices.DeleteAsync(x => ids.Contains(x.Id));
        }

        public async Task<OnboardingReviewDto> GetOnboardingReviewByIdAsync(int id)
        {
            var entity = await _onboardingReviewServices.GetAsync(id);
            return ObjectMapper.Map<OnboardingReviewModel, OnboardingReviewDto>(entity);
        }

        public async Task<int> UpdateOnboardingReviewAsync(OnboardingReviewDto onboardingReviewDto)
        {
            if (onboardingReviewDto.Id <= 0)
                throw new ArgumentException("入职申请ID无效");

            var entity = await _onboardingReviewServices.FindAsync(onboardingReviewDto.Id);
            if (entity == null)
                throw new ArgumentException($"未找到ID为{onboardingReviewDto.Id}的入职申请");

            ObjectMapper.Map(onboardingReviewDto, entity);
            await _onboardingReviewServices.UpdateAsync(entity);
            return entity.Id;
        }


    }
}
